'use strict';

app.controller('RoleCtrl',
    ['$scope', '$http', 'dialogs', '$state', 'RoleService',
        function ($scope, $http, dialogs, $state, RoleService) {
            var vm = $scope.vm = this;
            var defaultQuery = {};
            vm.query = angular.copy(defaultQuery);

            vm.roleFlexigridObj = {};
            vm.options = RoleService.gridOptions();

            //查询
            vm.search = function () {
                var param = angular.copy(vm.query);
                param[param.searchType] = param.keyword;
                delete param.searchType;
                delete param.keyword;
                vm.roleFlexigridObj.flexOptions({
                    newp: 1,
                    extParam: param
                }).flexReload();
            }

            // 重置
            vm.reset = function () {
                vm.query = angular.copy(defaultQuery);
                vm.search();
            }

            // 新增
            vm.addRole = function () {
                $state.go('setting.role.add');
            }
            // 修改
            vm.updateRole = function (roleId) {
                // 如果只选择一条记录，获取该记录的信息并显示修改页面
                if (ISS.isNotEmpty(roleId)) {
                    $state.go('setting.role.update', {id: roleId});
                } else {
                    dialogs.notify(undefined, getMessage("common.plzSelectOneRecourd"));
                }
            }
            // 删除
            vm.delRole = function (index) {
                var data = vm.roleFlexigridObj.getRawRecords(index);
                // 弹出确认删除提示信息
                var dlg = dialogs.confirm(undefined, getMessage("common.deleteConfirm"));
                dlg.result.then(function (param) {
                    RoleService.deleteRole(data).then(function (data) {
                        // 刷新列表
                        vm.roleFlexigridObj.flexReload();
                        //弹出提示信息
                        dialogs.notify(data);
                    });
                });
            }
        }]);

app.controller('RoleAddOrUpdateCtrl', ['$scope', '$http', '$state' , 'dialogs', 'RoleService',
    function ($scope, $http, $state, dialogs, RoleService) {
        var vm = $scope.vm = this;
        vm.role = {id: ''};
        vm.title = 'role.addRole';
        var privileges = [];

        function cancel() {
            $state.go('setting.role.list');
        }

        if (!ISS.isEmpty($state.params.id)) {
            vm.title = 'role.updateRole';
            vm.role.id = $state.params.id;
            RoleService.initUpdateRole(vm.role.id).then(function (data) {
                if (data.success === false) {
                    dialogs.notify(data);
                    cancel();
                } else {
                    vm.role = data;
                    vm.role.privileges = privileges;
                }
            });
        }

        vm.treeObj = {};
        vm.treeSetting = {
            async: {
                url: ISS.getPath('/role/treeRole?roleId=' + vm.role.id),
                enable: true,
                dataFilter: ajaxDataFilter
            },
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onAsyncSuccess: function () {
                    //vm.treeObj.expandAll(true);
                    onClickNode();
                },
                onCheck: onClickNode,
                onNodeCreated: function (event, treeId, treeNode) {
                    //获取id为0的节点，即权限
                    var node = vm.treeObj.getNodeByParam("id", 0);
                    //展开其下一级节点
                    vm.treeObj.expandNode(node, true, false, false);
                    if (ISS.isNotEmpty(vm.role.roleId)) {
                        vm.treeObj.checkNode(node, true, false);
                    }
                }
            }
        };

        //回调
        function onClickNode(event, treeId, treeNode) {
            vm.role.privileges = [];
            var checkedNodes = vm.treeObj.getCheckedNodes(true);
            angular.forEach(checkedNodes, function (node) {
                var privilege = {id: node.id};
                if (node.chkDisabled == true) {
                    privilege.use = false;
                } else {
                    privilege.use = true;
                }
                if (node.id != 0) {
                    vm.role.privileges.push(privilege);
                }
            });
            privileges = angular.copy(vm.role.privileges);

        };

        //在获取到的节点中添加跟加点
        function ajaxDataFilter(treeId, parentNode, responseData) {
            var root = {id: 0, name: ISS.getMessage('role.rolePermission')};
            angular.forEach(responseData, function (node) {
                if (node.checked) {
                    root.checked = true;
                }
            });
            responseData.push(root);
            return responseData;
        };

        vm.cancel = function () {
            cancel();
        };
        // 提交数据
        vm.save = function () {
            if (ISS.isEmpty(vm.role.privileges)) {
                dialogs.notify(null, getMessage("role.selectTree"));
                return;
            }
            if (ISS.isEmpty(vm.role.id)) {
                RoleService.addRole(vm.role).then(function (data) {
                    if (data.success) {
                        dialogs.notify(data).closed.then(function () {
                            cancel();
                        });
                    } else {
                        dialogs.notify(data);
                    }
                });
            } else {
                RoleService.updateRole(vm.role).then(function (data) {
                    if (data.success) {
                        dialogs.notify(data).closed.then(function () {
                            cancel();
                        });
                    } else {
                        dialogs.notify(data);
                    }
                });
            }
        };

    }]);
